package com.stormrage.edu.serv.learn.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.learn.LeaStudentCourseDao;
import com.stormrage.edu.serv.edu.EduCourseServ;
import com.stormrage.edu.serv.learn.LeaStudentCourseServ;
import com.stormrage.edu.serv.learn.LeaStudentSelectServ;
import com.stormrage.edu.serv.learn.LeaTermNessServ;
import com.stormrage.entity.edu.EduCourse;
import com.stormrage.entity.learn.LeaStudentSelect;
import com.stormrage.entity.learn.LeaTermNess;
import com.stormrage.entity.learn.req.LeaCourseStudyReq;
import com.stormrage.entity.learn.req.LeaSelectReq;
import com.stormrage.entity.learn.req.LeaStudentCourseReq;
import com.stormrage.entity.learn.view.LeaStudentCourse;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: LeaStudentCourseServImpl.java
 * @Package com.stormrage.edu.serv.learn.impl
 * @Description: 选课 Service
 * @author www.stormragetech.com
 * @date 2016年4月21日
 * @version V1.0
 */
@Service
public class LeaStudentCourseServImpl implements LeaStudentCourseServ {

	private static final Logger logger = LoggerFactory.getLogger(LeaStudentCourseServImpl.class);

	@Autowired
	private LeaStudentCourseDao leaStudentCourseDao;

	@Autowired
	private EduCourseServ eduCourseServ;

	@Autowired
	private LeaStudentSelectServ leaStudentSelectServ;

	@Autowired
	private LeaTermNessServ leaTermNessServ;

	@Override
	public Integer create(final LeaStudentCourse arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaStudentCourseServ 】- 【 create 】-【 STARTED 】");

		Integer index = leaStudentCourseDao.create(arg0);

		logger.debug(" 【 Serv 】- 【 LeaStudentCourseServ 】- 【 create 】-【 COMPLETED 】");
		return index;

	}

	@Override
	public void delete(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaStudentCourseServ 】- 【 delete 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("stuId", arg0);

		leaStudentCourseDao.delete(map);

		logger.debug(" 【 Serv 】- 【 LeaStudentCourseServ 】- 【 delete 】-【 COMPLETED 】");

	}

	@Override
	public Integer queryCountByTermAcct(final Integer acctId, final Integer termId) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaStudentCourseServ 】- 【 queryCountByTermAcct 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("acctId", acctId);
		map.put("termId", termId);

		Integer count = leaStudentCourseDao.queryCountByTermAcct(map);

		logger.debug(" 【 Serv 】- 【 LeaStudentCourseServ 】- 【 queryCountByTermAcct 】-【 COMPLETED 】");
		return count;

	}

	@Override
	public List<LeaStudentCourse> queryListByStu(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaStudentCourseServ 】- 【 queryListByStu 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("stuId", arg0);

		List<LeaStudentCourse> array = leaStudentCourseDao.queryListByStu(map);

		logger.debug(" 【 Serv 】- 【 LeaStudentCourseServ 】- 【 queryListByStu 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<LeaStudentCourse> queryListByStuNess(final LeaStudentCourseReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaStudentCourseServ 】- 【 queryListByStuNess 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("stuId", arg0.getStuId());
		map.put("ness", arg0.getNess());

		List<LeaStudentCourse> array = leaStudentCourseDao.queryListByStuNess(map);

		logger.debug(" 【 Serv 】- 【 LeaStudentCourseServ 】- 【 queryListByStuNess 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public void select(final LeaSelectReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaStudentCourseServ 】- 【 select 】-【 STARTED 】");

		List<LeaStudentCourse> sa = queryListByStu(arg0.getStuId());
		if (null != sa && sa.size() > 0) {
			delete(arg0.getStuId()); // 如果存在则删除所有已选课程
		}

		List<EduCourse> array = eduCourseServ.queryListByMajorId(arg0.getMajorId());

		for (int i = 0; i < arg0.getCount(); i++) {

			EduCourse course = eduCourseServ.queryById(Integer.valueOf(arg0.getList()[i]));

			LeaStudentCourse entity = LeaStudentCourse.instance(arg0.getStuId(), course.getId(), 0);
			create(entity);

			array.remove(course);

		}

		Iterator<EduCourse> it = array.iterator();
		while (it.hasNext()) {
			EduCourse course = it.next();
			LeaStudentCourse entity = LeaStudentCourse.instance(arg0.getStuId(), course.getId(), 1);
			create(entity);
		}

		LeaStudentSelect stuSelect = leaStudentSelectServ.queryByStuId(arg0.getStuId());
		stuSelect.setSelected(1);
		leaStudentSelectServ.update(stuSelect);

		logger.debug(" 【 Serv 】- 【 LeaStudentCourseServ 】- 【 select 】-【 COMPLETED 】");

	}

	@Override
	public Boolean checkSelect(final LeaCourseStudyReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 LeaStudentCourseServ 】- 【 checkSelect 】-【 STARTED 】");

		Boolean rs = true;

		Integer count = queryCountByTermAcct(arg0.getAcctId(), arg0.getTermId());
		LeaTermNess termNess = leaTermNessServ.queryByTermId(arg0.getTermId());

		if (null == termNess || !count.equals(termNess.getNess())) {
			rs = false;
		}

		logger.debug(" 【 Serv 】-【 LeaStudentCourseServ 】- 【 checkSelect 】-【 COMPLETED 】");
		return rs;

	}

}
